home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: thads@csn.net (Thad Smith)
- Newsgroups: comp.std.c,comp.lang.c.moderated
- Subject: Re: Integral promotion.
- Date: 15 Feb 1996 09:09:53 -0600
- Organization: T3 Systems
- Sender: clc@solutions.solon.com
- Approved: clc@solutions.solon.com
- Message-ID: <4fvic1$ef1@solutions.solon.com>
- References: <4fstj7$2l6@solutions.solon.com>
- Reply-To: ThadSmith@acm.org
- NNTP-Posting-Host: solutions.solon.com
-
- In article <4fstj7$2l6@solutions.solon.com>,
- Rune Huseby <rune.huseby@gpi.telemax.no> wrote:
- :I got a problem understanding the rules on integral promotion in
- :the C language:
- :
- :Consider the following function:
- :
- :short test(short x1, short x2);
- :
- :int main(void)
- :{
- : short result;
- : result = test(1, 2);
- : return 0;
- :}
- :
- :short test(short x1, short x2)
- :{
- : short result;
- : result = x1 + x2; /* Warning: '=' : conversion from 'int '
- : to 'short ', possible loss of data */
- : return result;
- :}
- :
- :My compiler (Microsoft Visual C++ 4.0), automagically converts
- :my short-parameters to int's, even though all variables involved
- :are short.
-
- The parameters for a prototyped function are not converted. That is,
- x1 and x2 remain type short int.
-
- When they are used in most expressions which expect an int type they
- undergo integral promotion, regardless of the type of the other
- operands.
-
- :I know that the standard says that all arguments can
- :be converted to the biggest 'type' of all the arguments, but is
- :it correct that char's and short's always are promoted to int's,
- :without regard to the other arguments in the expression?
-
- It depends on the usage, but usually yes. Some exceptions would be
- operand of sizeof, ++, --, and simple assignment.
-
- Even though the operands are promoted in expressions, the compiler
- might not actually expand the arguments if the end results are the
- same. In the example code, the compiler is free to perform the
- addition using a short accumulation register, assuming the result,
- when assigned to a short, is the same as would occur with promotion
- and conversion to short int.
-
- :The reason I ask is that I am writing code that should be
- :portable from a 16-bits environment (where short and int are the
- :same size) to a 32-bits environment. (My 16-bits compiler does
- :not complain about the code).
-
- If the code is written correctly and assumptions are not made about
- the maximum values, e.g., that 1 << 16 == 0, you should be OK.
-
- Thad
-